package com.shanghaionstar.carmanage.presenter;

import android.content.Context;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.DataPackageEntity;
import com.shanghaionstar.carmanage.iview.ICarFlowDataView;
import com.shanghaionstar.carmanage.service.CarFlowDataService;

import rx.functions.Action1;

/**
 * Created by 6335yz on 2016/7/24.
 */
public class CarFlowDataPresenter extends BasePresenter {

    private static final float START_DEGREE = 0f;
    private static final float END_DEGREE = 180f;
    private ICarFlowDataView carFlowDataView;
    private BaseActivity baseActivity;
    private CarFlowDataService carFlowDataService;

    public CarFlowDataPresenter(IMVPScheduler mvpScheduler, BaseActivity baseActivity ,ICarFlowDataView carFlowDataView) {
        super(mvpScheduler);
        this.baseActivity = baseActivity;
        carFlowDataService = new CarFlowDataService(baseActivity);
        this.carFlowDataView = carFlowDataView;
    }

    public CarFlowDataPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        carFlowDataService = new CarFlowDataService(context);
    }

    @Override
    public void setView(IMVPView view) {
        super.setView(view);
        carFlowDataView = (ICarFlowDataView) view;
    }

    public void setFlowDataView(ICarFlowDataView view) {
        carFlowDataView = view;
    }

    public void getFlowData() {
        subscription = carFlowDataService.doGetFlowData()
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<DataPackageEntity>() {
                    @Override
                    public void call(DataPackageEntity s) {
                        //缓存4G套餐数据
//                        UserCenterCacheHelper.getInstance().setPackageEntity_4g(s);
                        if (carFlowDataView != null) {
                            carFlowDataView.showFlowData(s);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (carFlowDataView != null && throwable != null) {
                            throwable.printStackTrace();
                            carFlowDataView.showErrorGetmsg(true,throwable);
                        }
                    }
                });
    }
}
